//依赖
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');

//引入路由
var indexRouter = require('./routes/index');
var loginRouter = require('./routes/login');
var registerRouter = require('./routes/reg');
var slRouter = require('./routes/sellerList');
var slinfoRouter = require('./routes/sellerInfo');
var orderRouter = require('./routes/order');
var menuRouter = require('./routes/menu');
var myorderRouter = require('./routes/myorder');
var myorder1Router = require('./routes/myorder1')
var submitRouter = require('./routes/submit');
var searchRouter = require('./routes/search');

var test1Router = require('./routes/test1');

//声明express实体
var app = express();

//配置
app.use(logger('dev'));
app.use(express.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();

//路由
app.all('*',function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Origin,Origin,Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    if (req.method === 'OPTIONS') {
        res.sendStatus(200); /让options请求快速返回/
    }
    else {
        next();
    }
});
app.use('/', indexRouter);
app.use('/login',loginRouter);
app.use('/reg',registerRouter);
app.use('/test',test1Router);
app.use('/sellerlist',slRouter);
app.use('/sellerinfo',slinfoRouter);
app.use('/order',orderRouter);
app.use('/menu',menuRouter);
app.use('/myorder',myorderRouter);
app.use('/myorder1',myorder1Router);
app.use('/submit',submitRouter);
app.use('/search',searchRouter);
app.use(function(req, res, next){
    res.sendStatus(404);
});

module.exports = app;
